<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms 
of the GNU General Public License as published by the Free Software Foundation; either 
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the 
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2006
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	Group2 class.
*
*	$Header: /cvsroot/wikindx/wikindx3/core/group/GROUP2.php,v 1.13 2006/10/06 07:52:58 sirfragalot Exp $
*****/
class GROUP2
{
// Constructor
	function GROUP2($db)
	{
		$this->db = $db;
	}
// Get institution names from WKX_institution.  Return associative array of id => institution.
	function grabAll()
	{
		include_once("core/html/HTML.php");
		$html = new HTML();
		include_once("core/messages/MESSAGES.php");
		$messages = new MESSAGES();
		$recordset = $this->db->select(array("WKX_institution"), array("id", "acronym","name"), 
			'ORDER BY ' . $this->db->tidyInputClause('name'));
		while($row = $this->db->loopRecordSet($recordset))
			$groups[$row['id']] = $html->dbToFormTidy($row['name']);
		if(isset($groups))
		{
// $groups[1] is hard-wired to 'General' in the database -- for display, change it to the required localization
			//$groups[1] = $messages->text('resources', 'general');
			return $groups;
		}

// shouldn't ever happen since 1 => 'General' should always be set.
		//return FALSE;
	}

// return a SQL condition clause when selecting by category
	function selectCondition($input)
	{
		$condition = '';
		$selectArray = explode(",", $input['category']);
// NB MySQL bug: http://bugs.mysql.com/bug.php?id=2047 -- append comma temporarily
		foreach($selectArray as $key => $value)
			$conditionArray[] = "FIND_IN_SET(" . $this->db->tidyInput($value) . ", CONCAT(" . 
			$this->db->formatfield('categories') . ", ','))";
		$condition .= implode(" " . $input['categoryMethod'] . " ", $conditionArray);
		return $condition;
	}
// Update $input to category table.  If category ID exists, update category name otherwise do nothing.
// $input is id => groupName
	function updateSql($input)
	{
		$recordset = $this->db->select(array("WKX_institution"), array("id", "acronym","name","address","website","country","comment"),"WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($input[0]));
		while($row = $this->db->loopRecordSet($recordset))
		{
			if($row['id']=$input[0])
			{
				$update['acronym'] = $input[1];
				$update['name'] = $input[2];
				$update['address'] = $input[3];
				$update['website'] = $input[4];
				$update['country'] = $input[5];
				$update['comment'] = $input[6];
				$this->db->update('WKX_institution', $update, 
				"WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($row['id']));
				unset($input[$row['id']]);
			}
		}
		return TRUE;
	}
// Add a new institution to the institution table
	function addSql($institution)
	{
// database match is case insensitive.
		$recordset = $this->db->select(array("WKX_institution"), array("id","name"), 
			"WHERE " . $this->db->formatField('id') . "=" . $this->db->tidyInput($institution[0]));
// If institution already exists quietly return without error.
		if($id = $this->db->fetchOne($recordset))
			return $id;
		$this->db->insert("WKX_institution", array('acronym','name','address','website','country','comment'), array($institution[1],$institution[2],$institution[3],$institution[4],$institution[5],$institution[6]));
		return TRUE;
	}
	
// Delete institution(s) from institution table.
	function deleteSql($institution)
	{
		foreach($institution as $value)
			$this->db->delete('WKX_institution', "WHERE " . $this->db->formatfield('id') . "=" . 
				$this->db->tidyInput($value));
		$this->db->optimize('WKX_institution');
		return TRUE;
	
	}	
	
}
?>
